<template>
  <div class="wrap">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div class="header_left">
          <img src="../../assets/1.jpg" alt="picture">
          <span class="header_text">电商管理后台</span>
        </div>
        <el-button type="info" plain @click="logout">退出</el-button>
      </el-header>
      <!-- 主体内容区 -->
      <el-container>
        <!-- 左侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 伸缩按钮 -->
          <div class="toggle" @click="handleToggle">
            <i class="iconfont icon-cebianshensuo_1" />
          </div>
          <el-menu
            background-color="#313743"
            text-color="#fff"
            active-text-color="#359bfd"
            :collapse="isCollapse"
            unique-opened
            :collapse-transition="false"
            :default-active="defaultActive"
            router
            @select="handleSelect"
            @open="menuOpen"
          >
            <!-- 左侧一级导航菜单(index 要独一无二且只接受一个字符串,这样打开才能只打开一个菜单) -->
            <el-submenu
              v-for="menu in menusList"
              :key="menu.id"
              :index="menu.id + ''"
            >
              <!-- 菜单名 -->
              <template slot="title">
                <i :class="iconObj[menu.id]" />
                <span :class="menuIndex == menu.id ? 'activeHeight' : ''">{{
                  menu.authName
                }}</span>
              </template>
              <!-- 二级菜单（index 是独一无二的且是可以实现路由跳转的路径） -->
              <el-menu-item
                v-for="item in menu.children"
                :key="item.id"
                :index="'/' + item.path"
              >
                <!-- 菜单名 -->
                <template slot="title">
                  <i class="el-icon-menu" />
                  <span>{{ item.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容区 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { removeItem, getItem, setItem } from '@/utils/storage';
import { menus } from '@/api/login.js';
export default {
  name: 'Home',
  components: {},
  data() {
    return {
      /* 一级菜单选中高亮 index */
      menuIndex: '',
      /* 二级菜单选中高亮*/
      defaultActive: '',
      /* 导航的折叠与展开 */
      isCollapse: false,
      /* 左侧导航菜单数据 */
      menusList: [],
      /* 导航菜单字体图标 */
      iconObj: {
        125: 'iconfont icon-haoyou',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-dingdan',
        145: 'iconfont icon-board-full'
      }
    };
  },
  mounted() {
    /* 左侧导航菜单 */
    menus()
      .then((res) => {
        const { data } = res;
        if (data.meta.status !== 200) return this.$message.error(data.meta.msg);
        this.menusList = data.data;
      })
      .catch((err) => {
        console.log(err);
      });
    /* 页面一二级菜单高亮 */
    this.defaultActive = getItem('url') ? getItem('url') : '';
    this.menuIndex = getItem('index') ? getItem('index') : '';
  },
  methods: {
    /* 一级菜单选中时高亮的 index */
    menuOpen(index) {
      setItem('index', index); // 本地存储,防止页面刷新丢失
      this.menuIndex = index;
    },
    /* 二级菜单选中时触发,并高亮 */
    handleSelect(url) {
      setItem('url', url); // 本地存储,防止页面刷新丢失
      this.defaultActive = url;
    },
    /* 退出登录 */
    logout() {
      removeItem('token');
      /* 返回登录页 */
      this.$router.back();
    },
    // 左侧导航折叠与展开
    handleToggle() {
      this.isCollapse = !this.isCollapse;
    }
  }
};
</script>
<style scoped lang='scss'>
.wrap {
  height: 100vh;
  overflow: hidden;
  .el-container {
    height: 100%;
    .el-header {
      background-color: #363d3f;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .header_left {
        display: flex;
        align-items: center;
        img {
          width: 50px;
          height: 50px;
          border-radius: 50%;
        }
        .header_text {
          color: #fff;
          font-size: 20px;
          margin-left: 15px;
        }
      }
    }

    .el-aside {
      background-color: #313743;
      color: #333;
      transition: all 0.3s;
      .toggle {
        height: 40px;
        line-height: 40px;
        text-align: center;
        .iconfont {
          font-size: 20px;
          color: white;
        }
      }
      .el-menu {
        border-right: 0;
      }
      .iconfont {
        margin-right: 10px;
      }
    }

    .el-main {
      background-color: #e9eef3;
      color: #333;
      overflow: hidden;
    }
  }
  .activeHeight {
    color: #359bfd;
  }
}
</style>
